package cn.study.controller;


import cn.study.entity.StudyCommodity;
import cn.study.entity.StudyDepartment;
import cn.study.entity.StudyRole;
import cn.study.service.OssService;
import cn.study.service.StudyCommodityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Controller
@Slf4j
@RequestMapping("/study-commodity")
public class StudyCommodityController {
    @Resource
    StudyCommodityService studyDepartmentService;
    @Resource
    OssService ossService;

    @GetMapping("/list")
    public String list(Model model, @RequestParam(value="pn",defaultValue="1") Integer pn,
                       @RequestParam(value="paginalsize",defaultValue = "5") Integer paginalsize)
    {
        PageHelper.startPage(pn, paginalsize);

        List<StudyCommodity> list=studyDepartmentService.getlist();
        PageInfo pInfo=new PageInfo(list,10);
        model.addAttribute("pInfo",pInfo);
        model.addAttribute("paginalsize",paginalsize);

        return "study_Commodity";
    }
    @GetMapping("/add")
    public String add()
    {

        return "study_Commodity_add";
    }

    @PostMapping("/Commodityadd")
    public String deptadd(StudyCommodity department,@RequestParam("file") MultipartFile file){
        String url=ossService.uploadFileAvatar(file);
        department.setCommodityImage(url);
        boolean bl=studyDepartmentService.save(department);
        if(bl){
            log.info("添加成功");
        }else {
            log.info("添加失败");
        }
        return "redirect:/study-commodity/list";
    }


    @GetMapping("del")
    public String del(Integer id)
    {
        boolean bl=studyDepartmentService.removeById(id);
        if(bl){
            log.info("删除成功");
        }else {
            log.info("删除失败");
        }
        return "redirect:/study-commodity/list";
    }
    @GetMapping("/upd")
    public String upd(Integer id,Model model)
    {
        StudyCommodity studyDepartment=studyDepartmentService.getById(id);
        model.addAttribute("list",studyDepartment);


        return "study_Commodity_upd";
    }

    @PostMapping("/Commodityupd")
    public String deptupd(Model model,StudyCommodity department,@RequestParam("file") MultipartFile file)
    {
        String url=ossService.uploadFileAvatar(file);
        department.setCommodityImage(url);
        studyDepartmentService.updateById(department);
        return "redirect:/study-commodity/list";
    }
}

